package com.ruoyi.back.service.impl

import cn.hutool.core.lang.func.Func
import com.baomidou.mybatisplus.extension.kotlin.KtQueryWrapper
import com.baomidou.mybatisplus.extension.plugins.pagination.Page
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl
import com.ruoyi.app.common.Data
import com.ruoyi.app.common.Fail
import com.ruoyi.app.common.Ok
import com.ruoyi.back.domain.Function
import com.ruoyi.back.domain.FunctionSon
import com.ruoyi.back.domain.FunctionListVo
import com.ruoyi.back.mapper.FunctionMapper
import com.ruoyi.back.service.FunctionService
import com.ruoyi.common.core.page.TableSupport
import com.ruoyi.common.utils.bean.BeanUtils
import org.springframework.stereotype.Service

@Service
class FunctionServiceImpl : ServiceImpl<FunctionMapper, Function>(), FunctionService {

    override fun getFunctionUseCount(type: Int): Int =
        when (type) {
            1 -> getOne(
                KtQueryWrapper(Function::class.java)
                    .eq(Function::name, "文生图")
            )?.useCount ?: 0

            2 -> getOne(
                KtQueryWrapper(Function::class.java)
                    .eq(Function::name, "图生图")
            )?.useCount ?: 0

            else -> getOne(
                KtQueryWrapper(Function::class.java)
                    .eq(Function::name, "头像变卡通")
            )?.useCount ?: 0
        }

    override fun functionPage(): Page<FunctionListVo> =
        TableSupport.buildPageRequest().let {
            page(
                Page(1, 10),
                KtQueryWrapper(Function::class.java)
                    .eq(Function::pid, 0)
                    .orderByAsc(Function::orderNum)
            )
                .let { page ->
                    page.records.let {
                        Page<FunctionListVo>().apply {
                            BeanUtils.copyProperties(page, this)
                            records = it.map {
                                FunctionListVo().apply {
                                    BeanUtils.copyProperties(it, this)
                                    functions = list(
                                        KtQueryWrapper(Function::class.java)
                                            .eq(Function::pid, it.id)
                                    )
                                        .takeIf { it.isNotEmpty() }
                                        ?.map {
                                            FunctionSon().apply {
                                                BeanUtils.copyProperties(it, this)
                                            }
                                        }
                                }
                            }
                        }
                    }
                }
        }

    override fun showFunction(functionId: Long): Data<Boolean> =
        getById(functionId)?.run {
            Ok("更改状态成功", updateById(this.apply {
                showStatus = if (showStatus == 0) 1 else 0
            }))
        } ?: kotlin.run { Fail(201, "不存在当前资源") }

    override fun saveFunction(function: Function): Data<Boolean> =
        function.id?.run {
            Ok("更新功能菜单成功", updateById(function))
        } ?: Ok("保存功能菜单成功", save(function))

    override fun removeFunction(ids: String): Data<Boolean> =
        Ok("移除成功", removeByIds(ids.split(",")))
}